package com.cloudkinto.utils;

import com.aspose.cells.Workbook;
import com.aspose.words.Document;
import com.aspose.words.SaveFormat;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.file.Files;
import java.util.UUID;

/**
 * @author long
 */
public class AsposePdfUtils {

    /**
     * word 转为 pdf 输出
     *
     * @param inPath  word文件
     * @param outPath pdf 输出文件目录
     */
    public static String word2pdf(String inPath, String outPath) {

        FileOutputStream os = null;
        try {
            String path = outPath.substring(0, outPath.lastIndexOf(File.separator));
            File file = new File(path);
            // 创建文件夹
            if (!file.exists()) {
                file.mkdirs();
            }
            // 新建一个空白pdf文档
            file = new File(outPath);
            os = new FileOutputStream(file);
            // Address是将要被转化的word文档
            Document doc = new Document(inPath);
            // 全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF,
            doc.save(os, SaveFormat.PDF);
            os.close();
        } catch (Exception e) {
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
            e.printStackTrace();
        }

        return outPath;
    }


    /**
     * word 转为 pdf 输出
     *
     * @param inPath word文件
     */
    public static ByteArrayOutputStream word2pdf(String inPath) {

        ByteArrayOutputStream os = null;
        try {
            os = new ByteArrayOutputStream();
            // Address是将要被转化的word文档
            Document doc = new Document(inPath);
            // 全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF,
            doc.save(os, SaveFormat.PDF);
            os.close();
        } catch (Exception e) {
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
            e.printStackTrace();
        }
        return os;
    }

    /**
     * word 转为 pdf 输出
     */
    public static ByteArrayOutputStream word2pdf(InputStream inputStream) {

        ByteArrayOutputStream os = null;
        try {
            os = new ByteArrayOutputStream();
            // Address是将要被转化的word文档
            Document doc = new Document(inputStream);
            // 全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF,
            doc.save(os, SaveFormat.PDF);
            os.close();
        } catch (Exception e) {
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
            e.printStackTrace();
        }
        return os;
    }

    /**
     * OutputStream 转 InputStream
     */
    public static ByteArrayInputStream parse(OutputStream out) {
        ByteArrayOutputStream baos = (ByteArrayOutputStream) out;
        return new ByteArrayInputStream(baos.toByteArray());
    }

    /**
     * InputStream 转 File
     */
    public static File inputStreamToFile(InputStream ins, String name) throws Exception {
        File file = new File(System.getProperty("java.io.tmpdir") + File.separator + name);
        if (file.exists()) {
            return file;
        }
        OutputStream os = Files.newOutputStream(file.toPath());
        int bytesRead;
        int len = 8192;
        byte[] buffer = new byte[len];
        while ((bytesRead = ins.read(buffer, 0, len)) != -1) {
            os.write(buffer, 0, bytesRead);
        }
        os.close();
        ins.close();
        return file;
    }

    /**
     * 根据网络地址获取 File 对象
     */
    public static File getFile(String url) throws Exception {
        String suffix = url.substring(url.lastIndexOf("."));
        HttpURLConnection httpUrl = (HttpURLConnection) new URL(url).openConnection();
        httpUrl.connect();
        return AsposePdfUtils.inputStreamToFile(httpUrl.getInputStream(), UUID.randomUUID() + suffix);
    }

    /**
     * excel 转为 pdf 输出
     *
     * @param inPath  excel 文件
     * @param outPath pdf 输出文件目录
     */
    public static String excel2pdf(String inPath, String outPath) {

        FileOutputStream os = null;
        try {
            String path = outPath.substring(0, outPath.lastIndexOf(File.separator));
            File file = new File(path);
            // 创建文件夹
            if (!file.exists()) {
                file.mkdirs();
            }
            // 新建一个空白pdf文档
            file = new File(outPath);
            os = new FileOutputStream(file);
            // Address是将要被转化的excel表格
            Workbook workbook = new Workbook(Files.newInputStream(new File(inPath).toPath()));
//			PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
//			pdfSaveOptions.setOnePagePerSheet(true);
            workbook.save(os, com.aspose.cells.SaveFormat.PDF);
            os.close();
        } catch (Exception e) {
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
            e.printStackTrace();
        }
        return outPath;
    }

    /**
     * excel 转为 pdf 输出
     *
     * @param inPath excel 文件
     */
    public static ByteArrayOutputStream excel2pdf(String inPath) {

        ByteArrayOutputStream os = null;
        try {
            os = new ByteArrayOutputStream();
            // Address是将要被转化的excel表格
            Workbook workbook = new Workbook(Files.newInputStream(new File(inPath).toPath()));
//			PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
//			pdfSaveOptions.setOnePagePerSheet(true);
            workbook.save(os, com.aspose.cells.SaveFormat.PDF);
            os.close();
        } catch (Exception e) {
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
            e.printStackTrace();
        }
        return os;
    }

    /**
     * excel 转为 pdf 输出
     *
     * @param
     */
    public static ByteArrayOutputStream excel2pdf(InputStream inputStream) {

        ByteArrayOutputStream os = null;
        try {
            os = new ByteArrayOutputStream();
            // Address是将要被转化的excel表格
            Workbook workbook = new Workbook(inputStream);
//			PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
//			pdfSaveOptions.setOnePagePerSheet(true);
            workbook.save(os, com.aspose.cells.SaveFormat.PDF);
            os.close();
        } catch (Exception e) {
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
            e.printStackTrace();
        }
        return os;
    }
}
